package com.wuliu.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuliu.common.annotation.RepeatSubmit;
import com.wuliu.common.core.domain.ApiResult;
import com.wuliu.common.core.domain.PageResult;
import com.wuliu.common.core.domain.entity.BasCust;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DictUtils;
import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.common.utils.StringUtils;
import com.wuliu.system.domain.TspDrivingTrack;
import com.wuliu.system.domain.dto.applet.OpDrivingRecordDto;
import com.wuliu.system.domain.dto.applet.QryDrivingRecordDto;
import com.wuliu.system.domain.dto.applet.TspDrivingRecordDto;
import com.wuliu.system.domain.vo.applet.DrivingRecordBasicVo;
import com.wuliu.system.domain.vo.applet.ExportDrivingRecordVo;
import com.wuliu.system.domain.vo.applet.TspDrivingRecordCountVo;
import com.wuliu.system.domain.vo.applet.TspDrivingRecordVo;
import com.wuliu.system.service.FileUploadService;
import com.wuliu.system.service.ITspDrivingTrackService;
import com.wuliu.system.service.applet.AppletTspDrivingRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.wuliu.common.core.controller.BaseController;
import com.wuliu.common.core.domain.AjaxResult;
import com.wuliu.system.domain.TspDrivingRecord;
import com.wuliu.common.utils.poi.ExcelUtil;
import com.wuliu.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 行车记录Controller
 *
 * @author realue
 * @date 2023-11-04
 */
@Api(tags = {"行车记录控制器"})
@RestController
@RequestMapping("/applet/driving")
public class TspDrivingRecordController extends BaseController {
    @Resource
    private AppletTspDrivingRecordService tspDrivingRecordService;
    @Resource
    private FileUploadService fileUploadService;
    @Resource
    private ITspDrivingTrackService tspDrivingTrackService;

    /**
     * 查询行车记录列表
     */
    @ApiOperation(value = "查询行车记录")
    @GetMapping("/list")
    public ApiResult<PageResult<TspDrivingRecordVo>> list(QryDrivingRecordDto dto) {
        return ApiResult.success(tspDrivingRecordService.selectTspDrivingRecordList(dto));
    }

    /**
     * 导出行车记录列表
     */
    @ApiOperation(value = "导出行车记录")
    @PostMapping("/export")
    public void export(HttpServletResponse response, QryDrivingRecordDto dto) {
        List<ExportDrivingRecordVo> list = tspDrivingRecordService.exportTspDrivingRecordList(dto);
        ExcelUtil<ExportDrivingRecordVo> util = new ExcelUtil<ExportDrivingRecordVo>(ExportDrivingRecordVo.class);
        util.exportExcel(response, list, "行车记录数据");
    }

    /**
     * 获取行车记录详细信息
     */
    @ApiOperation(value = "行车记录详细")
    @GetMapping(value = "/{id}")
    public ApiResult<TspDrivingRecordVo> getInfo(@PathVariable("id") String id) {
        return ApiResult.success(tspDrivingRecordService.selectTspDrivingRecordById(id));
    }

    /**
     * 新增行车记录
     */
    @RepeatSubmit
    @ApiOperation(value = "新增行车记录")
    @PostMapping("/add")
    public ApiResult<Integer> add(@RequestBody @Validated TspDrivingRecordDto dto) {
        return ApiResult.success(tspDrivingRecordService.insertTspDrivingRecord(dto));
    }

    /**
     * 修改行车记录
     */
    @ApiOperation(value = "编辑行车记录")
    @PostMapping("/edit")
    public ApiResult<Integer> edit(@RequestBody @Validated TspDrivingRecordDto dto) {
        return ApiResult.success(tspDrivingRecordService.updateTspDrivingRecord(dto));
    }

    /**
     * 删除行车记录
     */
    @ApiOperation(value = "删除行车记录")
    @DeleteMapping("/{ids}")
    public ApiResult<Integer> remove(@PathVariable String[] ids) {
        return ApiResult.success(tspDrivingRecordService.deleteTspDrivingRecordByIds(ids));
    }

    @RepeatSubmit
    @PostMapping("/addImages")
    @ApiOperation(value = "添加行车记录图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "images", value = "图片链接数组", dataType = "array", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "记录id", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型 21.货物图片 22.车辆(车牌)图片 23.发车公里数图片 24.收车公里数图片 24.加油图片 25.高速费图片", dataType = "String", paramType = "query")
    })
    public ApiResult<Boolean> addImages(String[] images, String id, Integer type) {
        return ApiResult.success(tspDrivingRecordService.addImages(images, id, type));
    }

    @PostMapping("/uploadFile")
    @ApiOperation(value = "上传文件", notes = "请求格式:multipart/form-data")
    @ApiImplicitParam(name = "file", value = "上传文件", required = true, dataType = "MultipartFile", dataTypeClass = MultipartFile.class, paramType = "query")
    public ApiResult<String> uploadFile(@RequestPart("file") MultipartFile file) {
        return ApiResult.success(fileUploadService.uploadFile(file, ""));
    }

    @GetMapping("/getImages")
    @ApiOperation(value = "查询行车记录图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "记录id", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型 21.货物图片 22.车辆(车牌)图片 23.发车公里数图片 24.收车公里数图片 24.加油图片 25.高速费图片", dataType = "String", paramType = "query")
    })
    public ApiResult<List<String>> getImages(String id, Integer type) {
        return ApiResult.success(tspDrivingRecordService.getImages(id, type));
    }

    @GetMapping("/getTrainNum")
    @ApiOperation(value = "获取车次号")
    @ApiImplicitParam(name = "drivingDate", value = "日期", dataType = "string", paramType = "query")
    public ApiResult<String> getTrainNum(String drivingDate) {
        return ApiResult.success(tspDrivingRecordService.getTrainNum(drivingDate));
    }
//
//    @GetMapping("/getProjType")
//    @ApiOperation(value = "获取项目类型")
//    public ApiResult<List<String>> getProjType() {
//        String[] projType = {"京东传站", "京东城配", "京东TC", "京东企配", "京东德邦", "京东支线", "京东摆渡", "拼多多",
//                "美团", "极兔", "顺丰", "佰大", "三好果品", "博贺蔬菜", "粤西农批蘑菇",
//                "零食有鸣", "赵一鸣零食", "电白零食舱", "回头货", "其他"};
//        return ApiResult.success(Arrays.asList(projType));
//    }
//
//    @GetMapping("/getClassNum")
//    @ApiOperation(value = "获取班次")
//    public ApiResult<List<String>> getClassNum() {
//        String[] classNum = {"早班车", "早班加车", "午班车", "午班加车", "夜班车"};
//        return ApiResult.success(Arrays.asList(classNum));
//    }

    @GetMapping("/getBasic")
    @ApiOperation(value = "获取基础信息")
    public ApiResult<DrivingRecordBasicVo> getBasic() {
        BasCust cust = SecurityUtils.getBasCust();
        DrivingRecordBasicVo basicVo = new DrivingRecordBasicVo();
        String projType = DictUtils.getSysConfigValue(cust.getSysAccount(), "driving_proj_type");
        if (StringUtils.isNotEmpty(projType)) {
            String[] projTypes = projType.split(",");
            basicVo.setProjTypes(Arrays.asList(projTypes));
        }
        String classNum = DictUtils.getSysConfigValue(cust.getSysAccount(), "driving_class_num");
        if (StringUtils.isNotEmpty(classNum)) {
            String[] classNums = classNum.split(",");
            basicVo.setClassNums(Arrays.asList(classNums));
        }
        return ApiResult.success(basicVo);
    }

    @GetMapping("/count")
    @ApiOperation(value = "统计表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "beginDt", value = "开始日期", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "endDt", value = "结束日期", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型 1.按司机 2.按车辆 3.按站点 4.按线路 5.按车队", dataType = "integer", paramType = "query")
    })
    public ApiResult<List<TspDrivingRecordCountVo>> count(String beginDt, String endDt, Integer type) {
        if (StringUtils.isAnyBlank(beginDt, endDt)) {
            throw new ServiceException("开始日期和结束日期不能为空");
        }
        return ApiResult.success(tspDrivingRecordService.count(beginDt, endDt, type));
    }

    @GetMapping("/getTracks/{recordId}")
    @ApiOperation(value = "查询行车记录操作日志")
    @ApiImplicitParam(name = "recordId", value = "行车记录id", dataType = "integer", paramType = "path")
    public ApiResult<List<TspDrivingTrack>> getTracks(@PathVariable("recordId") String recordId) {
        return ApiResult.success(tspDrivingTrackService.list(new LambdaQueryWrapper<TspDrivingTrack>()
                .eq(TspDrivingTrack::getRecordId, recordId)
                .orderByAsc(TspDrivingTrack::getOpDate)));
    }

    @PostMapping("/opRecord")
    @ApiOperation(value = "操作行车记录")
    public ApiResult<Boolean> opRecord(@RequestBody @Validated OpDrivingRecordDto dto) {
        return ApiResult.success(tspDrivingRecordService.opRecord(dto));
    }
}
